<?php

namespace app\common\model;

use app\common\exception\BusinessException;

class UserCourse extends Base
{
    /**
     * 设置题库有效期
     * @param $uid
     * @param $course_id
     * @throws
     */
    public function setExpire($uid, $course_id)
    {
        // 查询条件
        $where = [
            'uid' => $uid,
            'course_id' => $course_id,
        ];

        // 查询题库信息
        $courseInfo = $this->where($where)->find();

        // 根据题库存在是否设置有效期
        $time = time();
        if ($courseInfo) {
            $now_expire = $courseInfo['expire'] >= $time ? $courseInfo['expire'] : $time;
            $now_expire = $this->getTimeByExpireType($courseInfo['expire'], $courseInfo['expire_type'], $now_expire);
            $saveData = [
                'expire' => $now_expire,
            ];
            $isUpdate = $this->where($where)->save($saveData);
            if ($isUpdate === false) {
                throw new BusinessException('更新用户题库表失败');
            }
        } else {
            $now_expire = $this->getTimeByExpireType($courseInfo['expire'], $courseInfo['expire_type'], $time);
            $saveData = [
                'uid' => $uid,
                'course_id' => $course_id,
                'expire' => $now_expire,
                'a_time' => $time,
                'u_time' => $time,
            ];
            $isUpdate = $this->save($saveData);
            if ($isUpdate === false) {
                throw new BusinessException('新增用户题库表失败');
            }
        }
    }

    /**
     * getTimeByExpireType
     * @param $expire
     * @param $expireType
     * @param $nowTime
     * @return false|int
     * @throws BusinessException
     */
    public function getTimeByExpireType($expire, $expireType, $nowTime)
    {
        switch ($expireType) {
            case 1:
                $action = '+' . $expire . ' second';
                break;
            case 2:
                $action = '+' . $expire . ' minute';
                break;
            case 3:
                $action = '+' . $expire . ' hour';
                break;
            case 4:
                $action = '+' . $expire . ' day';
                break;
            case 5:
                $action = '+' . $expire . ' month';
                break;
            case 6:
                $action = '+' . $expire . ' year';
                break;
            default:
                throw new BusinessException('不支持的有效期类型');
        }
        return strtotime($action, $nowTime);
    }

    /**
     * 查看是否拥有此题库
     * @param $uid
     * @param $course_id
     * @return bool
     */
    public function checkHasCourse($uid, $course_id): bool
    {
        // 查询条件
        $time = time();
        $where = [
            ['uid', '=', $uid],
            ['course_id', '=', $course_id],
            ['expire', '>', $time]
        ];

        // 查询题库信息
        $count = $this->where($where)->count();
        if ($count) {
            return true;
        }

        // 查询开放平台
        if ((new User())->checkIsOpenPlatformUser($uid)) {
            return true;
        }

        // 查询VIP信息检查
        return (new UserVip())->checkIsCourseVip($uid, $course_id);
    }
}
